package org.MobileSales.DataManagement;

import java.util.ArrayList;
import java.util.List;

import org.MobileSales.Model.Clients.ClientGroup;
import org.MobileSales.Model.Orders.Order;
import org.MobileSales.Model.Orders.OrderItem;
import org.MobileSales.Model.Orders.OrderStatus;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class OrdersManagement {
	public Context context;
	
	public OrdersManagement(Context c)
	{
		this.context = c;
	}
	
	public Order GetOrder(int orderId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, OrderStatusId, ClientId, Date" 
							+ " FROM Orders" 
							+ " WHERE _id = " + orderId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;

     		Order result = new Order();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.ClientId = c.getInt(c.getColumnIndex("ClientId"));
     		result.Date = c.getString(c.getColumnIndex("Date"));
     		result.Status = GetOrderStatus(c.getInt(c.getColumnIndex("OrderStatusId")));
     		result.Items = GetOrderItemList(result.Id);
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public OrderItem GetOrderItem(int orderItemId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, ProductId, OrderId, Quantity" 
							+ " FROM OrderItems" 
							+ " WHERE _id = " + orderItemId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		OrderItem result = new OrderItem();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.Product = new ProductsManager(context).GetProduct(c.getInt(c.getColumnIndex("ProductId")));
     		result.OrderId = c.getInt(c.getColumnIndex("OrderId"));
     		result.Quantity = c.getDouble(c.getColumnIndex("Quantity"));
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public Iterable<OrderItem> GetOrderItemList(int orderId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id" 
							+ " FROM OrderItems" 
							+ " WHERE OrderId = " + orderId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		List<OrderItem> result = new ArrayList<OrderItem>();
     		
     		while (c!=null){
     			result.add(GetOrderItem(c.getInt(c.getColumnIndex("_id"))));
     			c.moveToNext();
     		}
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public void StoreOrderItem(OrderItem orderItem)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
			
			cv.put("ProductId",orderItem.Product.Id);
			cv.put("OrderId", orderItem.OrderId);
			cv.put("Quantity", orderItem.Quantity);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (orderItem.Id != 0)
				db.update("OrderItems", cv, "_id ="+orderItem, null);
			else
				db.insert("OrderItems", null, cv);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void RemoveOrderItem(int orderItemId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
			db.delete("OrderItems", "_id ="+orderItemId, null);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public OrderStatus GetOrderStatus(int orderStatusId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, Name" 
							+ " FROM OrderStatus" 
							+ " WHERE _id = " + orderStatusId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		OrderStatus result = new OrderStatus();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.Name = c.getString(c.getColumnIndex("Name"));
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	public void StoreOrderStatus(OrderStatus orderStatus)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
			
			cv.put("Name",orderStatus.Name);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (orderStatus.Id != 0)
				db.update("OrderStatus", cv, "_id ="+orderStatus, null);
			else
				db.insert("OrderStatus", null, cv);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void RemoveOrderStatus(int orderStatusId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
			db.delete("OrderStatus", "_id ="+orderStatusId, null);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
}
